<template>
  <div class="iot">
    <div>
      <label>{{ $t("route.DeviceManager") }}</label>
      <div></div>
      <button>
        <img /><label>{{ $t("route.IOT_data") }}</label>
      </button>
    </div>

    <div>
      <el-radio-group @change="getDeviceProList" v-model="dataType">
        <el-radio-button :label="$t('route.energy_consumption')"></el-radio-button>
        <el-radio-button :label="$t('route.Crop_rate')"></el-radio-button>
        <el-radio-button :label="$t('route.Working_hours')"></el-radio-button>
      </el-radio-group>
      <div>
        <el-cascader
          style="width: 200px"
          :options="proLineOptions"
          @change="proLineChange"
        />
        <el-date-picker
          style="margin-left: 10px; width: 150px"
          @change="getDeviceProList"
          v-model="query.createTime"
          type="date"
          :placeholder="$t('route.Select_date')"
        />
      </div>
    </div>
    <div style="margin-top: 20px">
      <div v-if="chartShow" id="myChart" style="height: 250px; width: 100%"></div>
    </div>
    <div>
      <el-table :data="tableData" :row-style="{ height: '60px' }">
        <el-table-column prop="noseCode" :label="$t('route.Head_Model')" />
        <el-table-column prop="kind" :label="$t('route.kind')" />
        <el-table-column
          prop="electric"
          :formatter="formatele"
          :label="$t('route.Power_consumption')"
        />
        <el-table-column
          prop="Ratio"
          :formatter="formatrat"
          :label="$t('route.Crop_rate')"
        />
        <el-table-column
          prop="workDur"
          :formatter="formatwork"
          :label="$t('route.Working_hours')"
        />
        <el-table-column
          prop="leisurem"
          :formatter="formatlei"
          :label="$t('route.Idle_time')"
        />
        <el-table-column
          prop="bootUpDur"
          :formatter="formatboot"
          :label="$t('route.Startup_duration')"
        />
      </el-table>
    </div>
  </div>
</template>

<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import Constant from "../../js/common/constant.js";
import * as echarts from "echarts";
export default {
  data() {
    return {
      proLineOptions: [],
      query: {
        total: 0,
        workshop: null,
        proLine: null,
        createTime: new Date(),
      },
      chartShow: false,
      dataType: this.$t("route.energy_consumption"),
      xdata: [],
      ydata: [],
      device: {},
      tableData: [],
      months: ["2022-8", "2022-9", "2022-10", "2022-11"],
      monthday: "2022-11",
    };
  },
  methods: {
    proLineChange(value) {
      this.query.workshop = value[0];
      this.query.proLine = value[1];
      this.query.pageNum = 1;
      this.getDeviceProList();
    },
    getDeviceProList() {
      http.post(
        api.GET_DEVICE_PROLIST3,
        {
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
          workshop:
            this.query.workshop == this.$t("route.All_workshops")
              ? null
              : this.query.workshop,
          proLine:
            this.query.proLine == this.$t("route.All_groups") ? null : this.query.proLine,
          createTime: this.query.createTime.format("yyyy-MM-dd"),
        },
        (data) => {
          this.tableData = data.data;
          var xData = [];
          var extXData = [];
          var yData = [];
          for (var item of data.data) {
            xData.push(item.noseCode);
            extXData.push(item.kind);
            if (this.dataType == this.$t("route.energy_consumption")) {
              yData.push(item.electric.toFixed(2));
            } else if (this.dataType == this.$t("route.Crop_rate")) {
              item.bootUpDur == 0
                ? yData.push(0)
                : yData.push(((item.workDur / item.bootUpDur) * 100).toFixed(1));
            } else {
              yData.push(Number(item.workDur / 3600).toFixed(1));
            }
          }
          var myChart = echarts.init(document.getElementById("myChart"));
          var option = null;
          if (this.dataType == this.$t("route.energy_consumption")) {
            option = CommonUtils.initBarMultXOption(
              this.$t("route.energy_consumption"),
              xData,
              yData,
              extXData
            );
          } else if (this.dataType == this.$t("route.Crop_rate")) {
            option = CommonUtils.initBarMultXOption(
              this.$t("route.Crop_rate") + "%",
              xData,
              yData,
              extXData
            );
          } else {
            option = CommonUtils.initBarMultXOption(
              this.$t("route.Working_hours"),
              xData,
              yData,
              extXData
            );
          }
          myChart.setOption(option);
        }
      );
    },

    formatlei(row) {
      return CommonUtils.formatDur(row.bootUpDur - row.workDur);
    },
    formatboot(row) {
      return CommonUtils.formatDur(row.bootUpDur);
    },
    formatwork(row) {
      return CommonUtils.formatDur(row.workDur);
    },
    formatrat(row) {
      return ((row.workDur / row.bootUpDur) * 100).toFixed(1) + "%";
    },
    formatele(row) {
      return row.electric.toFixed(1) + "kW·h";
    },
    getPorLineRow() {
      http.post(
        api.GET_POR_LINE_ROW,
        {
          adminId: this.$store.state.user.id,
        },
        (data) => {
          for (var item of data.data) {
            var children = item.children;
            children.unshift({
              value: this.$t("route.All_groups"),
              label: this.$t("route.All_groups"),
            });
          }
          this.proLineOptions = data.data;
          this.proLineOptions.unshift({
            value: this.$t("route.All_workshops"),
            label: this.$t("route.All_workshops"),
          });
        }
      );
    },
  },
  created() {
    this.chartShow = true;
    this.device = JSON.parse(CommonUtils.getParam(this, "device", "setDevice"));
    this.getPorLineRow();
    this.getDeviceProList();
  },
  destroyed() {
    this.chartShow = false;
  },
};
</script>

<style lang="scss" scoped>
.iot {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  > div {
    display: flex;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;
  }

  > div:nth-of-type(1) {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    > label:nth-of-type(1) {
      font-weight: 600;
    }

    > div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    > button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      > img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }
  }

  > div:nth-of-type(2) {
    display: flex;
    flex-direction: row;

    > div:nth-of-type(1) {
      flex: 1;
      text-align: center;
    }
  }

  > div:nth-of-type(3) {
    display: flex;
    flex-direction: column;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    > div:nth-of-type(1) {
      display: flex;
      flex-direction: row;
      align-items: center;

      > div:nth-of-type(1) {
        display: flex;
        flex-direction: row;
        align-items: center;
      }

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        > input:nth-of-type(1) {
          border: 1px solid #eeeeee;
          font-size: 15px;
          width: 150px;
          padding: 5px 8px;
          outline: none;
        }

        > button {
          color: #a866ff;
          margin-left: 10px;
          padding: 4px 10px;
        }
      }

      > div:nth-of-type(2),
      > div:nth-of-type(3) {
        margin-left: 10px;
      }
    }
  }
}
</style>
